import React, { useState, useMemo, useEffect } from 'react';
import { ColView, filter_list, useHigh, GridTable, filter_row, useCmd, WaitUmd, MainView, obj_str, BtTxt, PanelFind,
    InfoDlg, obj_int, uJob, cline, Btn, is_empty, loc } from 'sui';
import { fix_col, load_col } from 'warehouse/base/cmd';
import { DlgNum } from 'oil/task/ctl';
import { get_col, load_custom } from './task/cmd';
import { load_supply } from 'stock/base/cmd';
import { useAccess, User } from 'cache';

const OrderView = () => {
    const [open_info, openInfo] = useState(false);
    const [open_val, openVal] = useState(false);
    const [map_supply, mapSupply] = useState();
    const [map_custom, mapCustom] = useState();
    const [custom, setCustom] = useState();
    const [col, setCol] = useState();
    const [high] = useHigh(140, 100);
    const [no_acc] = useAccess();
    const umd = useCmd("file/list/qapart");

    useEffect(() => {
        load_col("partinp", setCol);
        load_custom(setCustom, mapCustom);
        load_supply("part", null, mapSupply);
    }, []);

    const getRow = useMemo(() => {
        const lst = filter_list(["sn"], col);
        return filter_row(umd.value, lst, umd.filter);
    }, [umd.value, umd.filter, col]);

    const pact = {
        "fin": () => openVal(true),
        "back": () => openVal(true),
    };

    const act = {
        "back": (v) => {
            umd.startWait();

            const cmd = {
                line: cline(),
                k: obj_str(umd.cur, "order"),
                sn: obj_str(umd.cur, "sn"),
                ngson: "",
                path: "qapart",
                num: obj_int(v, "dnum"),
                uname: User.uName(),
            };

            uJob(null, "sale/toback", cmd,
                r => {
                    umd.reload(umd.endWait);
                    openInfo(true);
                },
                e => umd.setErr());
        },
        "fin": (v) => {
            umd.startWait();

            const cmd = {
                line: cline(),
                tp: "part",
                sn: obj_str(umd.cur, "sn"),
                num: obj_int(v, "dnum"),
                uname: User.uName(),
            };

            uJob(null, "stock/accept", cmd, r => umd.reload(umd.endWait), e => umd.setErr());
        },
    };

    if (umd.isFin("col")) {
        const list = [
            {sn: "partinp", name: loc("l_781"), fix: fix_col()},
        ];

        const hreturn = () => {
            load_col("partinp", setCol);
            umd.hreturn();
        };

        return <ColView hreturn={hreturn} title={loc("l_781")} subtitle={loc("l_221")} list={list} cur="partinp"/>;
    }

    const getCol = () => {
        if (is_empty(col)) {
            return null;
        }

        const tmp = [
            {name: loc("l_193"), key: 'index', width: 70, frozen: true},
            {name: loc("l_238"), key: 'e_opt', width: 130, frozen: true, formatter: props => {
                const d = props.row;
                return (
                    <div className="inline-flex">
                        <BtTxt hcmd={() => umd.pact(pact, d, "fin")} disabled={no_acc("ptc")}>{loc("l_105")}</BtTxt>
                        <BtTxt hcmd={() => umd.pact(pact, d, "back")} disabled={no_acc("ptc")}>{loc("l_111")}</BtTxt>
                    </div>
                );
            }},
        ];

        get_col(tmp, col, map_supply, map_custom, custom);

        return tmp;
    };

    const Right = () => (
        <div className="inline-flex">
            <Btn className="mr-4" hcmd={() => umd.pact(null, null, "col")} disabled={no_acc("ptc")}>{loc("l_221")}</Btn>
        </div>
    );

    return (
        <MainView>
            <PanelFind left={loc("l_781")} right={<Right/>} hcmd={v => umd.setFilter(v)}/>
            <GridTable col={getCol()} row={getRow} high={high}/>
            <DlgNum open={open_val} hcmd={v => umd.act(act, v)} setOpen={openVal} data={umd.cur} mod="part" col={col} fin={umd.fin}/>
            <WaitUmd umd={umd}/>
            <InfoDlg open={open_info} setOpen={openInfo} msg={loc("l_859")}/>
        </MainView>
    );
};

export {OrderView};
